package com.tandashi.springbootstudy01.service.impl;

import com.tandashi.springbootstudy01.dao.master.TargetDataDao;
import com.tandashi.springbootstudy01.entity.master.TargetData;
import com.tandashi.springbootstudy01.service.TargetDataService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (TargetData)表服务实现类
 *
 * @author makejava
 * @since 2023-04-08 21:49:04
 */
@Service
public class TargetDataServiceImpl implements TargetDataService {
    @Resource
    private TargetDataDao targetDataDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TargetData queryById(Long id) {
        return this.targetDataDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param targetData 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<TargetData> queryByPage(TargetData targetData, PageRequest pageRequest) {
        long total = this.targetDataDao.count(targetData);
        return new PageImpl<>(this.targetDataDao.queryAllByLimit(targetData, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param targetData 实例对象
     * @return 实例对象
     */
    @Override
    public TargetData insert(TargetData targetData) {
        this.targetDataDao.insert(targetData);
        return targetData;
    }

    /**
     * 修改数据
     *
     * @param targetData 实例对象
     * @return 实例对象
     */
    @Override
    public TargetData update(TargetData targetData) {
        this.targetDataDao.update(targetData);
        return this.queryById(targetData.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.targetDataDao.deleteById(id) > 0;
    }
}
